//
//  NBLocationUtils.m
//  automobiledashboard
//
//  Created by 王建华 on 2025/9/26.
//

#import "NBLocationUtils.h"

@interface NBLocationUtils ()

@property (nonatomic, strong) CLLocation *lastLocation;
@property (nonatomic, strong) CLHeading *lastHeading;
@property (nonatomic, strong) ItineraryModel *itineraryArray;

@end

@implementation NBLocationUtils

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)startUpdatingLocationAndHeading {
    
    [self.locationManager requestWhenInUseAuthorization]; // 请求定位权限
    [self.locationManager startUpdatingLocation];   // 开始获取位置更新
    [self.locationManager startUpdatingHeading];    // 开始获取方向更新
}

- (void)stopUpdatingLocationAndHeading {
    [self.locationManager stopUpdatingLocation];
    [self.locationManager stopUpdatingHeading];
}

// 位置更新
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    
    if (locations.count > 0) {
        CLLocation *location = [locations lastObject];
        self.lastLocation = location;
        if (self.delegate && [self.delegate respondsToSelector:@selector(locationManager:didUpdateLocations:didUpdateHeading:)]) {
            
            [self.delegate locationManager:manager
                        didUpdateLocations:self.lastLocation
                          didUpdateHeading:self.lastHeading];
        }
        // 记录位置信息
        LocationsModel *model = [[LocationsModel alloc] init];
        model.location = location;
        if (self.itineraryArray.locationArray.count > 0) {
            CLLocation *lastLocation = [self.itineraryArray.locationArray lastObject].location;
            CLLocationDistance distance = [lastLocation distanceFromLocation:location];
            if (distance >= 10) {
                [self saveLocationWithModel:model];
            }
        }else {
            [self saveLocationWithModel:model];
        }
    }
    NSArray *locationsArr = [LocationsModel searchWithWhere:nil];
    NSArray *itineraryArr = [ItineraryModel searchWithWhere:nil];
    NSLog(@"位置信息：%@", locationsArr);
    NSLog(@"行程信息：%@", itineraryArr);
}

- (void)saveLocationWithModel:(LocationsModel *)model {
    [model saveToDB];
    [self.itineraryArray.locationArray addObject:model];
    [self.itineraryArray updateToDB];
}

// 朝向更新
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    
    self.lastHeading = newHeading;
    if (self.delegate && [self.delegate respondsToSelector:@selector(locationManager:didUpdateLocations:didUpdateHeading:)]) {
        
        [self.delegate locationManager:manager
                    didUpdateLocations:self.lastLocation
                      didUpdateHeading:self.lastHeading];
    }
}

- (CLLocationManager *)locationManager {
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
        
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest; // 设置定位精度
    }
    return _locationManager;
}

- (ItineraryModel *)itineraryArray {
    if (!_itineraryArray) {
        _itineraryArray = [[ItineraryModel alloc] init];
        [_itineraryArray saveToDB];
    }
    return _itineraryArray;
}

+ (MKMapCamera *)updataCameraWithMapView:(MKMapView *)mapView
                                andSpeed:(CLLocationSpeed)speed
                              andHeading:(CLLocationDirection )heading {
    
    MKMapCamera *camera = mapView.camera;
    camera.centerCoordinate = mapView.userLocation.location.coordinate;
    camera.altitude = [NBLocationUtils calculateCameraAltitudeWithSpeed:speed];  // 设置相机与地图表面的距离为1000米 200 - 2000
    camera.pitch = [NBLocationUtils calculateCameraPitchWithSpeed:speed];      // 设置相机的倾斜角度为45度        30  - 63
    camera.heading = heading;
    
    [mapView setCamera:camera animated:YES];
    return camera;
}

+ (NSString *)updataSpeedStringFromSpeed:(CLLocationSpeed)speed {
    
    NSString *speedString = [NSString stringWithFormat:@"%.0f", speed < 0 ? 0 : speed*3.6];
    return speedString;
}

// 根据速度计算A的值
+ (CGFloat)calculateCameraPitchWithSpeed:(CGFloat)speed {
    if (speed < 100) {
        // 速度在0到100之间时，B随速度线性增加
        CGFloat startB = 30;
        CGFloat endB = 63;
        return startB + (endB - startB) * (speed / 100);
    } else {
        // 速度超过100时，B保持最大值63
        return 63;
    }
}

// 根据速度计算B的值
+ (CGFloat)calculateCameraAltitudeWithSpeed:(CGFloat)speed {
    if (speed < 100) {
        // 速度在0到100之间时，A随速度线性增加
        CGFloat startA = 200;
        CGFloat endA = 2000;
        return startA + (endA - startA) * (speed / 100);
    } else {
        // 速度超过100时，A保持最大值2000
        return 2000;
    }
}

@end
